import sys
import pygame
from pygame_Alian.bullet import Bullet

class Ship:
    def __init__(self, screen, settings, bullets):
        self.screen = screen
        self.image = pygame.image.load('images/ship.bmp')   # 加载图片
        self.screen_rect = screen.get_rect()
        self.rect = self.image.get_rect()

        # 放在底部中间
        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery
        self.rect.bottom  = self.screen_rect.bottom

        self.moving = False
        self.last_move = None
        self.on_fire = False
        self.bullets = bullets
        self.settings = settings

    # 在制定位置绘制
    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def move_left(self):
        self.last_move = self.move_left
        if self.screen_rect.left < self.rect.centerx:   # 限制活动范围
            self.rect.centerx -= 1

    def move_right(self):
        self.last_move = self.move_right
        if self.rect.centerx < self.screen_rect.right:
            self.rect.centerx += 1

    def move_up(self):
        self.last_move = self.move_up
        if self.screen_rect.top < self.rect.centery:
            self.rect.centery -= 1

    def move_down(self):
        self.last_move = self.move_down
        if self.rect.centery < self.screen_rect.bottom:
            self.rect.centery += 1

    def keep_moving(self):
        if self.moving and self.last_move:
            self.last_move()

    def fire(self):
        self.bullets.add(Bullet(self.settings, self.screen, self))


